import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
//制作插件，可用this.   ，节省每次在页面导入,第一步
import {postKeyValueRequest} from "./utils/api";
import {postRequest}  from "./utils/api";
import {getRequest} from "./utils/api";
import {putRequest} from "./utils/api";
import {deleteRequest} from "./utils/api";

import {initMenu} from "./utils/menus";
//制作插件 ：2步
Vue.prototype.postKeyValueRequest = postKeyValueRequest;
Vue.prototype.postRequest = postRequest;
Vue.prototype.getRequest = getRequest;
Vue.prototype.putRequest = putRequest;
Vue.prototype.deleteRequest = deleteRequest;

Vue.config.productionTip = false
Vue.use(ElementUI);
import Global from './Global';
Vue.use(Global);
// 路由前置守卫
//解决，登录后刷新页面，导航栏消失不在加载问题
// 参数1：要跳到哪个页面，参数2：从哪个页面来，参数3：执行跳转动作
router.beforeEach((to, from, next) => {
  // 如果要去登录页直接跳，如果是页面与页面之间的跳转，判断是否登录，如果登录跳转，没登录跳到登录页
  if (to.path =='/') {
    next();
  } else {
    //判断页面是否登录，登录跳转。未登录跳转到登录页
    if (window.sessionStorage.getItem("user")) {
      initMenu(router, store);
      next();
    } else {
      console.log(to);
      //跳转到登录页
      next('/');
      //跳转到登录，登录后直接跳转到输入的网址:还需要在login.vue中
      next('/?redirect='+to.path);
    }

  }
})
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
